﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library.Encrypt;
using VehicleScale.Models;
using VehicleScale.DataAccess;

namespace VehicleScale.Forms.Vehicles
{
    public partial class FrmAuthorize : TemplateForm
    {

        public FrmAuthorize()
        {
            InitializeComponent();

            this.btnLogin.Click += new EventHandler(delegate
            {
                EmployeeModel model = null;
                if (Authorize(ref model))
                {
                    if (model.GroupPermissionID == 1)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        Library.Tools.MsgBox.Show("Bạn cần phải có quyền Administrator.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    Library.Tools.MsgBox.Show("Tên / Mật khẩu đăng nhập của bạn không hợp lệ.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            });

            this.btnClose.Click += new EventHandler(delegate
            {
                this.Close();
            });
        }

        private bool Authorize(ref EmployeeModel model)
        {
            string username = this.edtUsername.Text;
            string password = Encrypt.Encode("MD5", this.edtPassword.Text);
            if (String.Compare(username, Common.MyAcc, true) == 0 &&
                String.Compare(this.edtPassword.Text, Common.MyPass, true) == 0)
            {
                model = new EmployeeModel();
                model.GroupPermissionID = 1;
                return true;
            }

            IEmployeeDataAccess dataAccess = new EmployeeDataAccess();
            model = dataAccess.GetBy(username, password);
            if (String.Compare(model.Username, "admin", true) == 0)
            {
                model.GroupPermissionID = 1;

            }
            return (model != null);
        }
    }
}
